﻿(function (wtm) {


    function getTreeData(objArray) {
        return $.map(objArray, function (item) {
            return {
                title: item.Text,
                id: item.Value,
                field: item.Text,
                href: item.Url,
                spread: item.Expended,
                checked: item.Selected,
                disabled: item.Disabled,
                children: getTreeData(item.Children)
            };
        });
    }

    wtm.treecontainer_Render = function (para) {

        para.url = wtm.getUrl(para.url);

        $.get(para.url, function (resp, state) {
            var treeItems = getTreeData(resp.Data);

            var option = {
                id: para.id,
                elem: '#wtTree_' + para.id,
                onlyIconControl: para.onlyIconControl,
                showCheckbox: para.showCheckbox,
                showLine: para.showLine,
                data: treeItems,
                click: function (data) {
                    var $el = data.elem;
                    $el.closest('.layui-tree').find('.layui-tree-set.layui-this').removeClass('layui-this');
                    $el.addClass('layui-this');

                    // 优先处理用户自定义事件
                    if (para.customClick.length > 0) {
                        eval(para.customClick + "(data);");
                        return;
                    }

                    // 触发表格搜索
                    if (para.gridId.length > 0) {
                        var searchPanelData = wtm.GetFormData(para.searchPanelId);
                        var w = {};
                        w[para.levelField] = $el.parents('.layui-tree-set').length;
                        w[para.idField] = data.data.id;

                        layui.table.reload(para.gridId, {
                            where: $.extend(w, searchPanelData)
                        });
                    }
                    else {
                        if (data.data.href != null && data.data.href != '') {
                            ff.LoadPage1(data.data.href, 'wtContainer_' + para.id);
                            if (para.afterLoadEvent.length > 0) {
                                eval(para.afterLoadEvent + "()");
                            }
                        }
                    }
                }
            };
            layui.tree.render(option);

            if (para.autoLoad) {
                if (para.autoLoadUrl.length > 0) {
                    ff.LoadPage1(para.autoLoadUrl, 'wtContainer_' + para.id);
                    if (para.afterLoadEvent.length > 0) {
                        eval(para.afterLoadEvent + "()");
                    }
                }
                else {
                    var $first = $('#wtTree_' + para.id).find('.layui-tree-txt').first();
                    $first.click();
                }
            }

        });




    };
})(wtm);